﻿using DocumentFormat.OpenXml.Lite.Core.Office;
using DocumentFormat.OpenXml.Lite.Shared;
using DocumentFormat.OpenXml.Lite.SpreadsheetML.Excel.Base;
using DocumentFormat.OpenXml.Lite.SpreadsheetML.Excel.RunProperties;
using DocumentFormat.OpenXml.Lite.SpreadsheetML.Excel.Shared;
using DocumentFormat.OpenXml.Lite.SpreadsheetML.Excel.Types;
using DocumentFormat.OpenXml.Lite.SpreadsheetML.Excel.WorksheetElements;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Samples.Excel
{
    class SharedStrings
    {
        public static void Create()
        {
            Workbook wb = DocumentManager.Instance.CreateSpreadsheet();

            SharedStringTable shared = wb.CreateSharedStringTable();

            shared.CreateSimpleStringItem("Hello world!");

            RichTextRun r = new RichTextRun();
            r.AddProperty(new Bold());
            r.Text = new SimpleText("Hello world!");
            StringItem si = new StringItem();
            si.Runs.Add(r);
            shared.StringItems.Add(si);

            Worksheet sheet1 = wb.CreateWorksheet("Sheet1", null);

            Cell c1 = sheet1.CreateCell(1, 1);
            c1.CellType = ST_CellType.SharedString;
            c1.AddSimpleText("0");

            Cell c2 = sheet1.CreateCell(2, 1);
            c2.CellType = ST_CellType.SharedString;
            c2.AddSimpleText("1");

            wb.Save(@"samples\sharedstrings.xlsx");

            Workbook parsed = null;
            if (DocumentManager.Instance.OpenExcelDocument(@"samples\sharedstrings.xlsx", out parsed))
            {
                Console.WriteLine("Sample created and parsed succesfully!");
            }
            Console.ReadLine();
        }
    }
}
